<?php

namespace Bank;

use Products\ShortDeposit;
use Products\LongDeposit;
use Products\HomeLoan;
use Products\ConsumerLoan;
use Products\Checking;
use Products\DebitCard;
use Products\CreditCard;
use Customer\Customer;
use exceptions\BaseException;

class Bank
{
	
	private $name;
	private $address;
	private $deposits = array();
	private $loans = array();
  private $cards = array();
	private $availableMoney;
	private $reserve;
	
	
	public function __construct($name, $address, $availableMoney, $reserve)
	{
		$this->name = $name;
		$this->address = $address;
		
		if ($availableMoney <= 0 || !is_numeric($availableMoney)){
			throw new BaseException ("Available money should be a positive number!", BaseException::CODE_5001);
		}else{
			$this->availableMoney = $availableMoney;
		}
		
		if($reserve < 0.10*$availableMoney || !is_numeric($reserve)){
			throw new BaseException("Reserve should be a positive number and should be at least 10% of the bank available money!", BaseException::CODE_5002);
		}else{
			$this->reserve = $reserve;
		}
		
		
		
	}
	
	public function getAvailableMoney()
	{
		return $this->availableMoney;
	}
	
	public function getReserve()
	{
		return $this->reserve;
	}
	
	public function addShortDeposit(ShortDeposit $shortDeposit)
	{
		$this->deposits[] = $shortDeposit;
		$this->availableMoney = $shortDeposit->getAmount() + $this->availableMoney;
		$this->reserve = 0.10*$shortDeposit->getAmount() + $this->reserve;
	}
    public function addLongDeposit(LongDeposit $longDeposit)
	{
		$this->deposits[] = $longDeposit;
		$this->availableMoney = $longDeposit->getAmount() + $this->availableMoney;
		$this->reserve = 0.10*$longDeposit->getAmount() + $this->reserve;
	}
	
	public function payDepositInterest()
	{
		foreach($this->deposits as $deposit)
		{
			$deposit->addInterest();
		}
		
		
	}
	
  public function addHomeLoan(Customer $customer, $period, $amount)
	{
		$monthlyPayment = 0;
		
		foreach($customer->loans as $aLoan)
		{
			$monthlyPayment = $monthlyPayment + $aLoan->getMonthlyPayment();
		}
		
		$homeLoan = new HomeLoan($period, $amount);
		
		
		
		if(($homeLoan->monthlyPayment() + $monthlyPayment) > (1/2*$customer->getMonthSalary()))
		{

			throw new BaseException("Loan cannot be given to ".$customer->getName(), BaseException::CODE_5003);
			
		}elseif($amount > $this->availableMoney){
			throw new BaseException("Insufficient money available in bank!", BaseException::CODE_5004);
	
		}else{
			
			
			$customer->addLoan($homeLoan);
			$this->loans[] = $homeLoan;
			$this->availableMoney = $this->availableMoney - $amount;
		}
	}
	
	public function addConsumerLoan(Customer $customer, $period, $amount)
	{
		$monthlyPayment = 0;
	
		foreach($customer->loans as $aLoan)
		{
			$monthlyPayment = $monthlyPayment + $aLoan->getMonthlyPayment();
		}
	
		$consumerLoan = new ConsumerLoan($period, $amount);
	
	
	
		if(($consumereLoan->monthlyPayment() + $monthlyPayment) > (1/2*$customer->getMonthSalary()))
		{
	
			throw new BaseException("Loan cannot be given to ".$customer->getName(), BaseException::CODE_5003);
				
		}elseif($amount > $this->availableMoney){
			throw new BaseException("Insufficient money available in bank!", BaseException::CODE_5004);
	
		}else{
				
				
			$customer->addLoan($consumerLoan);
			$this->loans[] = $consumerLoan;
			$this->availableMoney = $this->availableMoney - $amount;
		}
	}
	
	
	public function showInfo()
	{
		echo "<p>Bank name is ".$this->name.".</p>
			 <p>Bank address is: ".$this->address.".</p>
			 <p>Bank available money is ".$this->availableMoney.".</p>
			 <p>Bank reserve is ".$this->reserve.".</p>";
		
		echo "<p><b>".$this->name." deposits and loans are: </b></p>";
		
		foreach($this->deposits as $deposit)
		{
			$deposit->showInfo();
		}
		foreach($this->loans as $loan)
		{
			$loan->showInfo();
		}
		
	}
	
	
}


?>